const { derived, Easing } = wx.worklet

export const GestureState = {
	POSSIBLE: 0,
	BEGIN: 1,
	ACTIVE: 2,
	END: 3,
	CANCELLED: 4,
}

export const AnimationStatus = {
	dismissed: 0,
	forward: 1,
	reverse: 2,
	completed: 3,
}

export const Curves = {
	fastLinearToSlowEaseIn: Easing.bezier(0.18, 1.0, 0.04, 1.0).factory(),
	linearToEaseOut: Easing.bezier(0.35, 0.91, 0.33, 0.97).factory(),
	easeInToLinear: Easing.bezier(0.67, 0.03, 0.65, 0.09).factory(),
	fastOutSlowIn: Easing.bezier(0.4, 0.0, 0.2, 1.0).factory(),
}

export const lerp = function (begin, end, t) {
	'worklet'
	return begin + (end - begin) * t
}

export const clamp = function (cur, lowerBound, upperBound) {
	'worklet'
	if (cur > upperBound) return upperBound
	if (cur < lowerBound) return lowerBound
	return cur
}

export function CurveAnimation({
	animation,
	animationStatus,
	curve,
	reverseCurve
}) {
	return derived(() => {
		'worklet'
		const useForwardCurve = !reverseCurve || animationStatus.value !== AnimationStatus.reverse
		const activeCurve = useForwardCurve ? curve : reverseCurve
		const t = animation.value
		if (!activeCurve) return t
		if (t === 0 || t === 1) return t
		return activeCurve(t)
	})
}